<template>
    <div>
        <div>登录</div>
        <el-input v-model="host">
            <template slot="prepend">Oauth服务器地址</template>
        </el-input>

        <el-input v-model="loginInfo.loginName">
            <template slot="prepend">用户名</template>
        </el-input>
        <el-input v-model="loginInfo.password">
            <template slot="prepend">密码</template>
        </el-input>
        <el-input v-model="loginInfo.validateCode">
            <template slot="prepend">验证码</template>
            <template slot="append"><img @click="img_click" :src="validateImg"/></template>

        </el-input>
        <el-button @click="login_click">登录</el-button>
        <el-button @click="jump_click" v-if="code">拿到了code, {{ code }} 返回!</el-button>
    </div>
</template>

<script setup>




defineOptions({
    name: "oauth-login",
    inheritAttrs: true,
});

//定义 vue2的this,当前组件
const $self = getCurrentInstance();

//定义 $attrs
const $attrs = useAttrs();



//定义data
const $data = reactive({"host":"http://192.168.5.213/api/mp-iam-admin-api","jump":false,"code":"","returnUrl":"","validateImg":"","loginInfo":{"token":"","appId":"","loginName":"admin","password":"1234","validateCode":""}});


const $props = defineProps({

});





const list = ref();

//定义事件
const $emit = defineEmits(["update:modelValue","loaded"]);



const getToken = function() {
    jv.$http.post($data.host + "/oauth/token").then(res => {
        var token = res.data.data;
        $data.loginInfo.token = token;
        img_click();
    });
};
const img_click = function() {
    $data.validateImg = $data.host + "/oauth/validate-img?_=" + jv.random() + "&token=" + $data.loginInfo.token;
};
const login_click = function() {
    jv.$http.post($data.host + "/oauth/login", $data.loginInfo).then(res => {
        var code = res.data.data;
        $data.code = code;
    });
};
const jump_click = function() {
    top.location = $data.returnUrl + "&code=" + $data.code;
};

onMounted( function() {
    $data.loginInfo.loginName = jv.me.user.loginName;
    $data.loginInfo.appId = jv.$route.query["app-id"];
    $data.returnUrl = jv.$route.query["return-url"];
    if (!$data.loginInfo.appId) {
        return jv.error("缺少参数 app-id")
    }
    if (!$data.returnUrl) {
        return jv.error("缺少参数 return-url")
    }
    getToken();
} )



</script>
